package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.OrderGoodsService;
import com.qf.service.OrderService;
import com.qf.service.UserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @Autowired
    private OrderGoodsService orderGoodsService;

    //订单列表
    // http://localhost:8083/admin/order/list?page=1&limit=20&sort=add_time&order=desc
    @GetMapping("/list")
    public Object list(Integer id, Integer userId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @RequestParam(defaultValue = "add_time") String sort,
                       @RequestParam(defaultValue = "desc") String order) {
        IPage<DtsOrder> orderIPage = orderService.queryByPage(id, userId, page, limit, sort, order);
        long total = orderIPage.getTotal();
        List<DtsOrder> items = orderIPage.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", items);
        return ResponseUtil.ok(map);
    }
    // 快递列表
    // 接口地址 : http://localhost:8083/admin/order/listShipChannel

//    @GetMapping("/listShipChannel")
//    public Object listShipChannel(){
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("shipChannelList",);
//        return ResponseUtil.ok(map);
//    }

    //##### . 订单详情
    //
    //- 接口地址 : http://localhost:8083/admin/order/detail?id=98

    @RequestMapping("/detail")
    public Object detail(Integer id){
        DtsOrder dtsOrder = orderService.queryById(id);

        Map<String, Object> map = new HashMap<>();

        List<DtsUser> dtsUsers = userService.listUs();
        DtsUser dtsUser = new DtsUser();
        for (int i = 0; i < dtsUsers.size(); i++) {
            if (dtsUsers.get(i).getId()==dtsOrder.getUserId()){
                dtsUser.setNickname(dtsUsers.get(i).getNickname());
                dtsUser.setAvatar(dtsUsers.get(i).getAvatar());
            }
        }

        List<DtsOrderGoods> orderGoodsList = orderGoodsService.list();
        for (int i = 0; i < orderGoodsList.size(); i++) {
          if (orderGoodsList.get(i).getOrderId()==id) {
              map.put("orderGoods",orderGoodsList.get(i));
          }
        }


        map.put("order",dtsOrder);
        map.put("user",dtsUser);

        return ResponseUtil.ok(map);
    }


}
